package com.it.controller;

import com.it.pojo.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

//测试请求参数接受
@RestController
public class RequestController {
//    @RequestMapping("/simpleParam")
//    public String simpleParam(HttpServletRequest request) {
//        String name = request.getParameter("name");
//        String ageStr = request.getParameter("age");
//
//        int age = Integer.parseInt(ageStr);
//        System.out.println(name+":"+age);
//        return "ok";
//    }

    //基于springboot方式
    @RequestMapping("/simpleParam")
    public String simpleParam(String name,Integer age) {
        System.out.println(name+":"+age);
        return "ok";
    }

    //2.实体参数
    @RequestMapping("/simplePojo")//注解来指定请求路径
    public String simplePojo(User user) {
        System.out.println(user);
        return "ok";
    }

    //2.实体参数
    @RequestMapping("/complePojo")//注解来指定请求路径
    public String complePojo(User user) {
        System.out.println(user);
        return "ok";
    }

    //3.数字集合参数
    @RequestMapping("/arrayParam")
    public String arrayParam(String[] args) {
        System.out.println(Arrays.toString(args));
        return "ok";
    }

    @RequestMapping("/listParam")
    public String listParam(@RequestParam List<String> list) {
        System.out.println(list);
        return "ok";
    }

    //4.日期时间参数
    @RequestMapping("/dateParam")
    public String dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime updateTime){
        System.out.println(updateTime);
        return "ok";
    }

    //5.json参数
    @RequestMapping("/jsonParam")
    //@RequestBody这个注解可以将json的请求数据封装到user这个实体对象当中
    public String jsonParam(@RequestBody User user){
        System.out.println(user);
        return "ok";
    }

    //6.路径参数
    @RequestMapping("/path/{id}")
    public String pathParam(@PathVariable Integer id){
        System.out.println(id);
        return "ok";
    }

    @RequestMapping("/path/{id}/{name}")
    public String pathParam2(@PathVariable Integer id ,@PathVariable String name){
        System.out.println(id+":"+name);
        return "ok";
    }
}